/**
 * @author Hank
 * @version 2012/6/9 
 */
(function(_w){
	var QZONE = _w.QZONE = _w.QZONE || {};
	
	var Drag = {
		isMousedown : 0,
		init : function(container, targetClass, dragStart, dragEnd, click){
			var me = this
			,mouseDownCb = function(evt){
				var id,
		            deltaX,deltaY,startX,startY,pX, pY,
		            isMoved = false,
		            clearSelected = false, 
		            isMutiSelect = false,
		            mutiDiv,
		            startInArea,
		            E = QZFL.event,
		            target = this,
		            item;
		        if(E.getButton(evt) != 0 || me.isMousedown)return false;
		        
		        var mouseUpCb = function(evt) {
		            var index, card;
		            if(isMoved){
		                //判断是否在某个容器上面
		                /*
		                if(ItemFactory.upOn(x, y)){
		                	
		                }*/
		                dragEnd && dragEnd.call(null, evt.clientX, evt.clientY);
		                ItemFactory.Move.backTo();
		            }else{
		                //单击
		                var id = target.getAttribute('data-id'),
		                	data = ItemFactory.getDataById(id);
		               	if(data.type == -1){ //文件夹
		               		//打开文件夹
		               		ItemFactory.openFolder(id);
		               	}else{
		               		if(data.type != 3){
		               			QZONE.FP && QZONE.FP.toApp(data.link);
		               		}
		               	}
		            }
		            E.removeEvent(document, 'mousemove', mouseMoveCb);
		            E.removeEvent(document, 'mouseup', mouseUpCb);
		            me.isMousedown = 0;
		            ItemFactory.Move.inited = 0;
		        };
		        
		        var mouseMoveCb = function(evt) {
		            var abs = Math.abs,
		                x = evt.clientX,
		                y = evt.clientY,
		                _x,_y;
		            if(ItemFactory.curFolder){
		            	return false;
		            }
		            if(abs(startX - x) > 3 || abs(startY - y) > 3){
		                isMoved = true;
		                _x = x - deltaX - pX;
		                _y = y - deltaY - pY;
		                if(!ItemFactory.Move.inited){
		                	id = target.getAttribute('data-id');
		                	ItemFactory.Move.init(id, _x, _y);
		                }else{ //初始化移动
		                	ItemFactory.Move.setPosition(_x, _y);
		                	//Drag.Move.PlaceHolder.insert();
		                	ItemFactory.Move.upOn(_x + 38, _y + 38);
		                }
		            }
		            E.preventDefault(evt);
		        };
		       
		        startX = evt.clientX;
		      	startY = evt.clientY;
		       	var pos = QZFL.dom.getXY(target),
		      	origX = pos[0],
		      	origY = pos[1];
		      	pos = QZFL.dom.getXY(target.parentNode);
		      	pX = pos[0];
		      	pY = pos[1];
		      	deltaX = startX - origX;
		     	deltaY = startY - origY;

		      	me.isMousedown = 1;	      
		        E.addEvent(document, 'mouseup', mouseUpCb);
		        E.addEvent(document, 'mousemove', mouseMoveCb);
		        E.preventDefault(evt);
			};
			QZFL.event.delegate(container, '.'+targetClass, 'mousedown', mouseDownCb);
		},
		destroy : function(container, targetClass){
			QZFL.event.undelegate(container, '.'+targetClass, 'mousedown');
		}
	};
	Drag.init($('finder_list_container'), 'item');
})(window);